package org.cunamas.web.util;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;

import org.cunamas.core.service.ReportesCunamasService;
import org.cunamas.core.service.impl.ReportesCunamasServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WebUtil {

	public static String retornaLabelSelectItem(List<SelectItem> list,String value){
		String label="";
		for (SelectItem selectItem : list) {
			if(value.equalsIgnoreCase((String)selectItem.getValue())){
				label=selectItem.getLabel();
				break;
			}
		}
		return label;
	}
	

	public static String retornaNombreMes(int m) {
		Locale locale = new Locale("es");
	    String month = "invalid";
	    DateFormatSymbols dfs = new DateFormatSymbols(locale);
	    String[] months = dfs.getMonths();
	    m=m-1;
	    if (m >= 0 && m <= 11 ) {
	        month = months[m];
	    }
	    return capitalizarString(month);
	}
	
	public static String capitalizarString(String string){
		return Character.toUpperCase(string.charAt(0))+string.substring(1, string.length()).toLowerCase();
	}
	
	public static ReportesCunamasService getReportesCunamasService(WebServiceContext context){
		ServletContext servletContext = (ServletContext) context.getMessageContext().get("javax.xml.ws.servlet.context");
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		return (ReportesCunamasService)webApplicationContext .getAutowireCapableBeanFactory().getBean(ReportesCunamasServiceImpl.class);
	}
	
	public static String retornaTrimestre(int trimestre){
		switch(trimestre){
			case 1: return "I";
			case 2: return "II";
			case 3: return "III";
			case 4: return "IV";
		}
		return null;
	}
	
	public static String retornaMesReferencia(String anio, String mes){
		Integer mesInt=Integer.valueOf(mes);
		String periodo=anio+(mesInt-1);		
		Calendar calendar1 = Calendar.getInstance();
		String periodoActual=String.valueOf(calendar1.get(Calendar.YEAR))+String.valueOf(calendar1.get(Calendar.MONTH));
		if(periodo.equalsIgnoreCase(periodoActual)){
			Calendar calendar2 = Calendar.getInstance();
			calendar2.set(Calendar.MONTH,calendar2.get(Calendar.MONTH)-2);
			return capitalizarString(new SimpleDateFormat("MMMM").format(calendar2.getTime()));
		}
		else{
			Calendar calendar3 = Calendar.getInstance();
			calendar3.set(Calendar.MONTH,mesInt-1);
			return capitalizarString(new SimpleDateFormat("MMMM").format(calendar3.getTime()));
		}
			
	}
	
	
	public static String retornaMesYAnioReferencia(String anio, String mes){
		String mesYAnio = "";
		String anioRef = "";
		String mesRef = "";
		
		DateFormat dfYear = new SimpleDateFormat("yyyy");
		
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		String fechaTexto = "01/" +  mes + "/" + anio;
		try{
			Date fecha = df.parse(fechaTexto);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(fecha);
			calendar.add(Calendar.MONTH, -2);
			
			Date fechaReferencia = calendar.getTime();
			
			anioRef = dfYear.format(fechaReferencia);
			mesRef = getMes(fechaReferencia.getMonth());
			
			mesYAnio = anioRef + "," + mesRef;
		}
		catch(Exception e){}
		
		return mesYAnio;
	}


	private static String getMes(int month) {
		String mesTexto = "";
		
		switch(month){
			case 0: mesTexto = "Enero"; break;
			case 1: mesTexto = "Febrero"; break;
			case 2: mesTexto = "Marzo"; break;
			case 3: mesTexto = "Abril"; break;
			case 4: mesTexto = "Mayo"; break;
			case 5: mesTexto = "Junio"; break;
			case 6: mesTexto = "Julio"; break;
			case 7: mesTexto = "Agosto"; break;
			case 8: mesTexto = "Setiembre"; break;
			case 9: mesTexto = "Octubre"; break;
			case 10: mesTexto = "Noviembre"; break;
			case 11: mesTexto = "Diciembre"; break;
		}
		
		return mesTexto;
	}
	
	public static void main(String[] args){
		String texto = retornaMesYAnioReferencia("2012", "01");
		System.out.println(texto);
	}
}
